//
//  RanIntroduceAnimationPageFour.m
//  test
//
//  Created by Ran on 15/10/29.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "RanIntroduceAnimationPageFour.h"

@interface RanIntroduceAnimationPageFour()

@property(nonatomic, weak)UILabel *titleLabel;
@property(nonatomic, weak)UILabel*detailLabel;

@property(nonatomic, strong)UIImageView *phoneImgView;
@property(nonatomic, strong)UIImageView *rocketImgView;

@end

@implementation RanIntroduceAnimationPageFour

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Private

- (void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    UIImageView *backgroudImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    [self addSubview:backgroudImageView];
    backgroudImageView.image = [UIImage imageNamed:@"KFC.bundle/4.bundle/bg-Portrait-568h"];
}

- (void)addAnimations
{
    CGFloat textAnimationDuration = 1;
    
    [self.titleLabel.layer addAnimation:({
        
        CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAni.fromValue = @1;
        opacityAni.toValue = @1;
        
        CAKeyframeAnimation *positionAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        positionAni.values = @[
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth * 1.5, kScreenHeight - kPercent * 130)],
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2 * 0.8, kScreenHeight - kPercent * 130)],
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2, kScreenHeight - kPercent * 130)]
                               ];
        positionAni.keyTimes = @[@0, @0.8, @1];
        
        CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
        aniGroup.animations = @[opacityAni, positionAni];
        aniGroup.duration = textAnimationDuration;
        aniGroup.removedOnCompletion = NO;
        aniGroup.fillMode = kCAFillModeForwards;
        
        aniGroup;
        
    }) forKey:nil];
    
    [self.detailLabel.layer addAnimation:({
        
        CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAni.fromValue = @1;
        opacityAni.toValue = @1;
        
        CAKeyframeAnimation *positionAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        positionAni.values = @[
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth * 1.5, kScreenHeight - kPercent * 90)],
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2 * 0.8, kScreenHeight - kPercent * 90)],
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2, kScreenHeight - kPercent * 90)]
                               ];
        positionAni.keyTimes = @[@0, @0.8, @1];
        
        CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
        aniGroup.animations = @[opacityAni, positionAni];
        aniGroup.duration = textAnimationDuration;
        aniGroup.removedOnCompletion = NO;
        aniGroup.fillMode = kCAFillModeForwards;
        
        aniGroup;
        
    }) forKey:nil];
    
    CGFloat phoneAnimationDuration = 0.6;
    [self.phoneImgView.layer addAnimation:({
        
        CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAni.fromValue = @1;
        opacityAni.toValue = @1;
        
        CABasicAnimation *positionAni = [CABasicAnimation animationWithKeyPath:@"position"];
        positionAni.fromValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth * 1.5, 262 * kPercent)];
        positionAni.toValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth * 0.5, 262 * kPercent)];
        
        CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
        aniGroup.animations = @[opacityAni, positionAni];
        aniGroup.duration = phoneAnimationDuration;
        aniGroup.removedOnCompletion = NO;
        aniGroup.fillMode = kCAFillModeForwards;
        
        aniGroup;
        
    }) forKey:nil];
    
    [self.rocketImgView.layer addAnimation:({
        
        CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAni.fromValue = @1;
        opacityAni.toValue = @1;
        
        CABasicAnimation *positionAni = [CABasicAnimation animationWithKeyPath:@"position"];
        positionAni.fromValue = [NSValue valueWithCGPoint:CGPointMake(95 * kPercent, 345 * kPercent)];
        positionAni.toValue = [NSValue valueWithCGPoint:CGPointMake(193 * kPercent, 265 * kPercent)];
        
        CABasicAnimation *scaleAni = [CABasicAnimation animationWithKeyPath:@"transform"];
        scaleAni.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 1)];
        scaleAni.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
        
        CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
        aniGroup.animations = @[opacityAni, positionAni, scaleAni];
        aniGroup.duration = phoneAnimationDuration;
        aniGroup.removedOnCompletion = NO;
        aniGroup.beginTime = CACurrentMediaTime() + phoneAnimationDuration;
        aniGroup.fillMode = kCAFillModeForwards;
        
        aniGroup;
        
    }) forKey:nil];
    
}

#pragma mark - Protocol

- (void)showAnimations
{
    [self reset];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self addAnimations];
    });
}


- (void)reset
{
    [self.subviews enumerateObjectsUsingBlock:^(UIView *subView, NSUInteger idx, BOOL *stop) {
        [subView.layer removeAllAnimations];
    }];
}

#pragma mark - Getter

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40 * kPercent)];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:30 * kPercent];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"肯德基宅急送";
        label.layer.opacity = 0;
        [self addSubview:label];
        self.titleLabel = label;
    }
    return _titleLabel;
}

- (UILabel *)detailLabel
{
    if (!_detailLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kPercent)];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:16 * kPercent];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"天天优惠,轻松e点餐!";
        label.layer.opacity = 0;
        [self addSubview:label];
        self.detailLabel = label;
    }
    return _detailLabel;
}

- (UIImageView *)imageViewWithName: (NSString *)imageName
{
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: imageName]];
    imgView.layer.opacity = 0;
    [self addSubview:imgView];
    return imgView;
}

- (UIImageView *)phoneImgView
{
    if (!_phoneImgView) {
        self.phoneImgView = ({
            UIImageView *imgView = [self imageViewWithName:@"KFC.bundle/4.bundle/1"];
            imgView.frame = CGRectMake(0, 0, kScreenWidth, 315 * kPercent);
            imgView;
        });
    }
    return _phoneImgView;
}

- (UIImageView *)rocketImgView
{
    if (!_rocketImgView) {
        self.rocketImgView = ({
            UIImageView *imgView = [self imageViewWithName:@"KFC.bundle/4.bundle/2"];
            imgView.frame = CGRectMake(0, 0, 222 * kPercent, 136 * kPercent);
            imgView;
        });
    }
    return _rocketImgView;
}

@end
